package com.ultrawise.survey.module.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.BaseService;
import com.ultrawise.survey.core.base.DaoException;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.user.service.UsersService;
import com.ultrawise.survey.module.user.vo.TSurveyInfoUsers;

/**
 * 前台业务查询管理
 * 
 */
@Controller
@RequestMapping(value = "/symanage/users")
public class UsersControl extends BaseContoller {
	@Autowired
	private UsersService usersService;

	@Autowired
	private BaseService baseService;

	/**
	 * 
	 * @throws ServiceException
	 * @throws DaoException
	 */
	@RequestMapping(value = "search.htm")
	public ModelAndView search(HttpServletRequest request, HttpSession session, ModelMap map) throws ServiceException {
		return new ModelAndView("userscontrol/search");
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	@RequestMapping(value = "changePass.htm")
	public String changePass(HttpSession session, HttpServletRequest request, TSurveyInfoUsers user)
			throws ServiceException {

		Gson jsonResp = new Gson();
		Map map = new HashMap();

		TSurveyInfoUsers dto = null;
		String oldpPass = request.getParameter("oldpPass");

		// 保存用户登录信息
		if (session.getAttribute(SurveyConstants.LOGINED_USER) != null) {
			TSurveyInfoUsers sessionUser = (TSurveyInfoUsers) session.getAttribute(SurveyConstants.LOGINED_USER);
			try {
				dto = usersService.getUser(sessionUser);
				if (dto.getUs_passwd() != null && dto.getUs_passwd().trim().equalsIgnoreCase(oldpPass)) {
					dto.setUs_passwd(user.getUs_passwd());

					usersService.savePassword(dto);
					map.put("success", "true");
					map.put("msg", "保存成功");
				} else {
					map.put("success", "false");
					map.put("msg", "旧密码输入错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", "false");
				map.put("msg", "保存失败");
			}
		} else {
			map.put("success", "false");
			map.put("msg", "没有登录或登录超时，请重新登录.");
		}

		request.setAttribute("content", jsonResp.toJson(map));

		return JSON_CONTENT_VIEW;
	}

	@RequestMapping(value = "getusers.htm")
	public ModelAndView getUserBygroup(HttpServletRequest request) {
		String groupid = request.getParameter("id");
		List<TSurveyInfoUsers> usls = new ArrayList<TSurveyInfoUsers>();
		try {
			usls = (List<TSurveyInfoUsers>) usersService.queryByColunm("GROUPID", groupid, new TSurveyInfoUsers(),
					TSurveyInfoUsers.class);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		request.setAttribute("usls", usls);
		return new ModelAndView("controlpanel/users/userlist");
	}

	/**
	 * 数据添加和保存页面的入口
	 * 
	 * @throws ServiceException
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "input.htm")
	public ModelAndView input(HttpServletRequest request) throws ServiceException {
		String UnitNo = request.getParameter("UnitNo");
		String UserNo = request.getParameter("UserNo");
		if (UserNo != null && !UserNo.equals("")) {
			TSurveyInfoUsers dto = (TSurveyInfoUsers) baseService.queryByID(UserNo, new TSurveyInfoUsers(),
					TSurveyInfoUsers.class);
			if (dto != null && !dto.equals("")) {
				request.setAttribute("dto", dto);
			}
		}
		request.setAttribute("UnitNo", UnitNo);
		return new ModelAndView("controlpanel/users/editUser");

	}

	/**
	 * 判断是否有该用户
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "isExis.htm")
	public String isExis(HttpServletRequest request, ModelMap map, HttpServletResponse response) throws IOException {
		String strLogin = request.getParameter("login");
		boolean con = usersService.isExistUser(strLogin);
		String com = "";
		if (con == true) {
			map.put("success", false);

		} else {
			map.put("success", true);
		}
		Gson jsonResp = new Gson();
		request.setAttribute("content", jsonResp.toJson(map));
		// response.getWriter().write(jsonResp.toJson(map));
		return JSON_CONTENT_VIEW;

	}

	/**
	 * 冻结一个用户
	 * 
	 * @throws ServiceException
	 */
	@RequestMapping(value = "del.htm")
	public String del(HttpServletRequest request, ModelMap map) throws ServiceException {
		String id = request.getParameter("id");
		if (id != null && !id.equals("")) {
			boolean com = usersService.delUser(id);
			if (com == true) {
				map.put("success", true);
			} else {
				map.put("success", false);
			}

		}
		Gson jsonResp = new Gson();
		request.setAttribute("content", jsonResp.toJson(map));
		return JSON_CONTENT_VIEW;

	}
}
